<template>
  <el-container style="padding: 15px">
    <el-header>
      <el-form size="small" label-width="70px">
        <el-row type="flex" :gutter="20" align="middle">
          <el-col :span="4">
            <el-form-item label="项目编号">
              <el-input v-model="searchObj.reportId" />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="项目名称">
              <el-input v-model="searchObj.reportName" />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="审核阶段">
              <el-select v-model="searchObj.leafId">
                <el-option v-for="item in leafList" :key="item.id" :value="item.id" :label="item.leafName" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="报告类型">
              <el-select v-model="searchObj.reportTypeId">
                <el-option :value="2" label="正评" />
                <el-option :value="1" label="预评" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="估价目的">
              <el-select v-model="searchObj.reportObjectiveId">
                <el-option v-for="item in objectiveList" :key="item.id" :value="item.id" :label="item.reportObjectiveName" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label-width="0px">
              <el-button type="primary" @click="getReportList">搜索</el-button>
              <el-button @click="clearSearch">重置</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-header>
    <el-divider />
    <el-main>
      <el-table :data="reportList">
        <el-table-column label="项目编号" prop="reportId" width="120px">
          <template v-slot="scope">
            <el-button type="text" size="small" @click="toDetails(scope.row.id)">{{ scope.row.reportId }}</el-button>
          </template>
        </el-table-column>
        <el-table-column label="项目名称" prop="reportName" width="300px" :formatter="subName" />
        <el-table-column label="项目状态" prop="leafName" />
        <el-table-column label="报告类型" prop="reportTypeName" />
        <el-table-column label="估价目的" prop="reportObjectiveName" />
      </el-table>
      <el-pagination
        :current-page="currentPage"
        :page-sizes="[10, 20, 40, 80]"
        :page-size="sizePage"
        layout="total, sizes, prev, pager, next, jumper"
        :total="totalPage"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-main>
  </el-container>
</template>

<script>
import { mapGetters } from 'vuex'
import { validObj } from '@/utils/validate'
import { getReportObjectiveNoPage } from '@/api/report'
import { getAuditStatusNoPage, getMyReportList, myReportListSearch } from '@/api/flow'
import { ellipsisString } from '@/utils'

export default {
  name: 'Already',

  data() {
    return {
      reportList: [],

      currentPage: 1,
      sizePage: 10,
      totalPage: 0,

      searchObj: {},

      leafList: [],
      objectiveList: []
    }
  },
  computed: {
    ...mapGetters([
      'id'
    ])
  },
  created() {
    this.init()
  },
  methods: {

    init() {
      this.getReportList()
      this.getSearchFormList()
    },
    getReportList() {
      const currentPage = this.currentPage
      const sizePage = this.sizePage
      const searchObj = this.searchObj
      if (validObj(searchObj)) {
        // 搜索
        myReportListSearch(this.id, currentPage, sizePage, searchObj).then(res => {
          console.log(res)
          this.reportList = res.data.list
          this.totalPage = res.data.total
        })
      } else {
        getMyReportList(this.id, currentPage, sizePage).then(res => {
          console.log(res)
          this.reportList = res.data.list
          this.totalPage = res.data.total
        })
      }
    },
    toDetails(id) {
      this.$router.push({ path: '/report/details', query: { id: id }})
    },
    clearSearch() {
      this.searchObj = {}
      this.getReportList()
      // 重置成功提示框
      this.$message({
        message: '重置成功',
        type: 'success'
      })
    },
    async getSearchFormList() {
      await getAuditStatusNoPage().then(res => {
        this.leafList = res.data.list
      })
      await getReportObjectiveNoPage().then(res => {
        this.objectiveList = res.data.list
      })
    },
    handleSizeChange(val) {
      this.sizePage = val
      this.getReportList()
    },

    handleCurrentChange(val) {
      this.currentPage = val
      this.getReportList()
    },
    subName(row, column, cellValue, index) {
      return ellipsisString(cellValue)
    }
  }
}
</script>

<style scoped>

</style>
